//
//  AfNetWorkingHandler.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/18.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "AfNetWorkingHandler.h"
#import <AFNetworking.h>
@implementation AfNetWorkingHandler
+(void)GetWithStr:(NSString *)str complection:(NETBLOCK)netBlock
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        netBlock(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"Error!: %@", error);
    }];

}
+(void)PostWithStr:(NSString *)str body:(NSString *)body complection:(NETBLOCK)network
{
    //将body体拆分成字典
    NSArray *arr = [body componentsSeparatedByString:@"&" ];
    NSMutableArray *jiArr = [NSMutableArray array];
    NSMutableArray *ouArr = [NSMutableArray array];
    for (NSInteger i = 0; i < arr.count; i++) {
        NSString *str = arr[i];
        NSArray *a = [str componentsSeparatedByString:@"="];
        [jiArr addObject:a.firstObject];
        [ouArr addObject:a.lastObject];
        
    }
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    for (NSInteger j = 0; j < arr.count; j++) {
        [dic setObject:ouArr[j] forKey:jiArr[j]];
    }
   
    //申明返回的结果是json类型
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //申明请求的数据是json类型
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:str parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //responseObject是NSdata 要转换成NSJSON
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        network(result);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"Error:%@", error);
    }];
    
#pragma mark - 网络判断
    // 1.获得网络监控的管理者
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        // 当网络状态改变了, 就会调用这个block
        switch (status) {
                
            case AFNetworkReachabilityStatusUnknown: // 未知网络
            {
                
                //                NSLog(@"未知网络");
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前为未知网络" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [alertView show];
                break;
            }
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
            {
                //                NSLog(@"没有网络(断网)");
                UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前无网络!请查看网络设置" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [alertView1 show];
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
            {
                //                NSLog(@"手机自带网络");
                UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前正在使用手机网络" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [alertView1 show];
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
            {
                NSLog(@"WIFI");
                
                break;
            }
        }
    }];
    
    // 3.开始监控
    [mgr startMonitoring];
 
}
@end
